OBJS = app_init.o 
OBJS += product/product.o
OBJS_ZCFG = app_init_zcfg.o
OBJS_ZCFG += product/product.o 

-include appconf.mak
-include $(TOPDIR)/.lib_config
-include $(TOPDIR)/rules.mak

CPPFLAGS += -I$(TOPDIR)/include -I./ -include lib_autoconf.h -DCONFIG_DATABUF_START=$(CONFIG_DATABUF_START) -DCONFIG_DMA_SIZE=$(CONFIG_DMA_SIZE) -DCONFIG_USB_CDC_ACM_DEV=$(CONFIG_USB_CDC_ACM_DEV) -I$(TOPDIR)/lib/lwip -I$(TOPDIR)/lib/lwip/include -I$(TOPDIR)/lib/lwip/include/ipv4


#LIBS = lib/libalink_agent.a lib/alinkv2_platform.a
LIBS = lib/libalink_agent.a $(LIBPATH)/alinkv2_platform.a
#LIBS_ZCFG = lib/libservice.a lib/alinkv2_platform.a 
LIBS_ZCFG = $(LIBPATH)/alinkv2_platform.a  lib/libservice.a 
LIBS_ZCFG +=lib/libos.a  lib/libsystem.a lib/libutility.a 
#LIBS_ZCFG += lib/cJSON.a 
#LIBS_ZCFG +=  lib/sds/libos.a  lib/sds/libapplication.a lib/sds/libsystem.a lib/sds/libutility.a lib/sds/libconnectivity.a 

LIBS += $(LIBPATH)/alink_axtls.a
LIBS_ZCFG += $(LIBPATH)/alink_axtls.a
CPPFLAGS += -I$(TOPDIR)/lib/axtls/ssl -I$(TOPDIR)/lib/axtls/config -I$(TOPDIR)/lib/axtls/crypto
ifdef CONFIG_WOLFSSL
LIBS += $(LIBPATH)/wolfssl.a
CPPFLAGS += -I$(TOPDIR)/lib/wolfssl
endif
#LIBS += $(LIBPATH)/sys.a $(LIBPATH)/wla.a
LIBS += $(LIBPATH)/sys.a $(LIBPATH)/wla_sta.a
#LIBS_ZCFG += $(LIBPATH)/sys.a $(LIBPATH)/wla.a
LIBS_ZCFG += $(LIBPATH)/sys.a $(LIBPATH)/wla_sta.a
ifdef CONFIG_LWIP
LIBS += $(LIBPATH)/lwip.a 
LIBS_ZCFG += $(LIBPATH)/lwip.a 
endif
LIBS += $(LIBPATH)/common.a
LIBS_ZCFG += $(LIBPATH)/common.a
ifdef CONFIG_FREERTOS
LIBS += $(LIBPATH)/freertos.a
LIBS += $(LIBPATH)/cJSON.a 
LIBS_ZCFG += $(LIBPATH)/freertos.a
LIBS_ZCFG += $(LIBPATH)/cJSON.a 
endif

all: all_app

app2: proj.ld $(OBJS:.o=.d) $(LIBS)
	$(ECHO) "\t [LD] $@"
	$(XCC) $(CFLAGS) $(LDFLAGS) -nostdlib -Wl,-e,app_main -T./proj.ld  -o $@ $(OBJS) $(LIBS)

app2.img: app2 app2.map app2.dis
	$(ECHO) "\t[IMG] $<"
	$(XOC) -R .bss -R .dma -O binary  $< $@

app_zcfg: proj.ld $(OBJS_ZCFG:.o=.d) $(LIBS_ZCFG)
	$(ECHO) "\t [LD] $@"
	$(XCC) $(CFLAGS) $(LDFLAGS) -nostdlib -Wl,-e,app_main -T./proj.ld  -o $@ $(OBJS_ZCFG) $(LIBS_ZCFG)

app_zcfg.img: app_zcfg app_zcfg.map
	$(ECHO) "\t[IMG] $<"
	$(XOC) -R .bss -R .dma -O binary  $< $@


app.img: app2.img im_header.bin $(TOPDIR)/utility/chksum
	@cat im_header.bin app2.img > $@.cs
	@$(TOPDIR)/utility/chksum $@.cs $@
	@rm -f *.bin *.cs app2.img

appZCFG.img: app_zcfg.img im_header_zcfg.bin $(TOPDIR)/utility/chksum
	@cat im_header_zcfg.bin app_zcfg.img > $@.tmp
	@$(TOPDIR)/utility/chksum $@.tmp $@.cs
	tr "\000" "\377" < /dev/zero | dd of=$@ ibs=1 count=192K &> /dev/null && sleep 1
	dd if=$@.cs of=$@ conv=notrunc &> /dev/null 
	@rm -f *.bin *.cs *.tmp app_zcfg.img

all_app: app.img appZCFG.img
	@mkalinkbin app.img appZCFG.img 
	mv alink_ota.img $(TOPDIR)/images/$(PROJ_NAME)_ota.img
	mv app.img $(TOPDIR)/images/$(PROJ_NAME).img
	mv appZCFG.img $(TOPDIR)/images/$(PROJ_NAME)_zcfg.img

clean:
	$(ECHO) "\t[CLEAN]"
	@find . \( -name '*.[osd]' -o -name '*.map' -o -name '*.bin' -o -name '*.dis' -o -name '*.ld' \) -type f -print| xargs rm -f
	@rm -f app2 im_header app.img.cs appZCFG.img.tmp appZCFG.img.cs
